using System;
using Ext;
using Ext.grid;
using Ext.util;
using ScriptFX;

namespace SampleScripts.grid {
    public class GroupSummary : Observable {
        public Ext.grid.GridPanel _grid;
        public ColumnModel _cm;
        public GridView _view;
        public Template _rowTpl;
        public Template _cellTpl;

        public GroupSummary(Dictionary config) {
            ExtClass.apply(this, config, null);
        }

        public void init(Ext.grid.GridPanel grid) {
            _grid = grid;
            _cm = grid.getColumnModel();
            _view = grid.getView();

            Type.InvokeMethod(_view, "afterMehod", "onColumnWidthUpdated", new Callback(doWidth), this);
            Type.InvokeMethod(_view, "afterMehod", "onAllColumnWidthsUpdated", new Callback(doAllWidths), this);
            Type.InvokeMethod(_view, "afterMehod", "onColumnHiddenUpdated", new Callback(doHidden), this);
            Type.InvokeMethod(_view, "afterMehod", "onUpdate", new Callback(doUpdate), this);
            Type.InvokeMethod(_view, "afterMehod", "onRemove", new Callback(doRemove), this);
            
            if (Script.IsNullOrUndefined(_rowTpl)) {
                _rowTpl = new Template(new string[] {
                    "<div class=\"x-grid3-summary-row\" style=\"{tstyle}\">",
                    "<table class=\"x-grid3-summary-table\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"{tstyle}\">",
                        "<tbody><tr>{cells}</tr>",
                    "</table></div>"
                });
                _rowTpl.disableFormats = true;
            }
            _rowTpl.compile();

            if (Script.IsNullOrUndefined(_cellTpl)) {
                _cellTpl = new Template(new string[] {
                    "<td class=\"x-grid3-col x-grid3-cell x-grid3-td-{id} {css}\" style=\"{style}\">",
                    "<div class=\"x-grid3-cell-inner x-grid3-col-{id}\" unselectable=\"on\">{value}</div>",
                    "</td>"
                });
                _cellTpl.disableFormats = true;
            }
            _cellTpl.compile();
        }

        public void toggleSummaries(bool visible) {
            Element el = _grid.getGridEl();
            if (!Script.IsNullOrUndefined(el)) {
                if (Script.IsUndefined(visible)) {
                    visible = el.hasClass("x-grid-hide-summary");
                }
                if (visible) {
                    el.removeClass("x-grid-hide-summary");
                } else {
                    el.addClass("x-grid-hide-summary");
                }
            }
        }

        public string renderSummary(Dictionary o, object[] cs) {
            if (Script.IsNullOrUndefined(cs)) {
                // private method
                cs = (object[])Type.InvokeMethod(_view, "getColumnData");
            }
            System.Array cfg = _cm.config;
            object[] buf = new object[0];
            return null;
        }

        public void doWidth() { }
        public void doAllWidths() { }
        public void doHidden() { }
        public void doUpdate() { }
        public void doRemove() { }
    }
}
